﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dunnice.BlogIndex.BLL.LocalSiteStorage
{
    /// <summary>
    /// 负责存储本地页面.
    /// </summary>
    public class PageStorage
    {
        public static void SavePage(string path, string pageConent)
        {
            string directoryName = GetGilePath(path);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            using (StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.Write),Encoding.UTF8))
            {
                sw.Write(pageConent);
                sw.Flush();
            }

            return;
        }

        private static string GetGilePath(string pagePath)
        {
            int index = pagePath.LastIndexOf('/');

            if (index > 0)
            {
                return pagePath.Substring(0, index);
            }

            return pagePath;
        }
    }
}
